package cn.itcast.wushangxiantu.BaseAdapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.UserData.FuBenFangJian;

public class FuBenFangJianAdapter extends BaseAdapter {
    private Context context;
    private FuBenFangJian fuBenFangJian;
    private ViewHolder viewHolder;
    private String newTime;
    private  SimpleDateFormat formatter;

    public FuBenFangJianAdapter(Context context, FuBenFangJian fuBenFangJian) {
        this.context = context;
        this.fuBenFangJian = fuBenFangJian;
        Date currentTime = new Date();
         formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         newTime = formatter.format(currentTime); // 当前本地时间
    }

    public FuBenFangJian getFuBenFangJian() {
        return fuBenFangJian;
    }

    public void setFuBenFangJian(FuBenFangJian fuBenFangJian) {
        this.fuBenFangJian = fuBenFangJian;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == fuBenFangJian.getFangJian()) {
            return 0;
        } else {
            return fuBenFangJian.getFangJian().size();
        }
    }

    @Override
    public Object getItem(int i) {
        return fuBenFangJian.getFangJian().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.adapter_duobaopaihang, null);
        viewHolder=new ViewHolder(view);
        viewHolder.tvName.setText(fuBenFangJian.getFangJian().get(i).getName());
        viewHolder.tvNum.setText("当前人数:"+fuBenFangJian.getFangJian().get(i).getNumber());
        viewHolder.tvTime.setText("门槛小怪："+fuBenFangJian.getFangJian().get(i).getMindj()+"级");
        long OTime = 0;
        long NTime = 0;
        try {
            OTime = formatter.parse(fuBenFangJian.getFangJian().get(i).getTime()).getTime();
            NTime = formatter.parse(newTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = (NTime - OTime);
        String time="剩余时间:";
        diff=720- diff/60000;
        time+=diff+"分钟";
        viewHolder.tvShuoMing.setText(time);
        return view;
    }

    class ViewHolder {
        public View rootView;
        public TextView tvName;
        public TextView tvNum;
        public TextView tvShuoMing;
        public TextView tvTime;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvNum = (TextView) rootView.findViewById(R.id.tvNum);
            this.tvShuoMing = (TextView) rootView.findViewById(R.id.tvShuoMing);
            this.tvTime = (TextView) rootView.findViewById(R.id.tvTime);
        }

    }
}
